package web;

import com.alibaba.fastjson.JSON;
import service.CourseService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;

public class AddQuestionServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Reader reader=new InputStreamReader(req.getInputStream(), StandardCharsets.UTF_8);
        BufferedReader bufferedReader=new BufferedReader(reader);
        String str="";
        String result="";
        while((str=bufferedReader.readLine())!=null){
            System.out.println(str);
            result+=str;
        }
        List<Question> questions= JSON.parseArray(result,Question.class);
        System.out.println(questions.get(0).problem);
        int lid=Integer.parseInt(req.getParameter("lid"));
        int difficulty=Integer.parseInt(req.getParameter("difficulty"));
        CourseService.addQuestions(questions,lid,difficulty);
    }
}

